﻿using System.Text;
using AdAggregator.Website;
using ClothingAggregator.Models.Parser;
using ClothingAggregator.Models.SearchData;
using ClothingAggregator.Utils;

namespace ClothingAggregator.Models.Website
{
    public class AnswearComWebsite : BaseWebsite<MySearchData, AnswearComSearchData, MyAdvertisement, AnswearComParser>
    {
        public AnswearComWebsite(AnswearComParser parser) : base(parser)
        {
        }

        protected override AnswearComSearchData MapSearchData(MySearchData searchData)
        {
            var answearSearchData = new AnswearComSearchData();
            MapCategory(answearSearchData, searchData.Sex, searchData.Category);
            MapColor(answearSearchData, searchData.Color);
            MapPrice(answearSearchData, searchData.PriceMin, searchData.PriceMax);
            MapSize(answearSearchData, searchData.Size);
            if (searchData.Age == ClothingAge.Child) answearSearchData.IsExactSearch = false;
            return answearSearchData;
        }

        private void MapSize(AnswearComSearchData searchData, ClothingSize size)
        {
            switch (size)
            {
                case ClothingSize.Any:
                    return;
                case ClothingSize.XXS:
                    {
                        searchData.Attributes.Add("rozmiar-xxs,519");
                        break;
                    }
                case ClothingSize.XS:
                    {
                        searchData.Attributes.Add("rozmiar-multi,506,602");
                        break;
                    }
                case ClothingSize.S:
                    {
                        searchData.Attributes.Add("rozmiar-multi,476,602");
                        break;
                    }
                case ClothingSize.M:
                    {
                        searchData.Attributes.Add("rozmiar-multi,324,508");
                        break;
                    }
                case ClothingSize.L:
                    {
                        searchData.Attributes.Add("rozmiar-multi,326,508,509");
                        break;
                    }
                case ClothingSize.XL:
                    {
                        searchData.Attributes.Add("rozmiar-multi,323,509");
                        break;
                    }
                case ClothingSize.XXL:
                    {
                        searchData.Attributes.Add("rozmiar-xxl,337");
                        break;
                    }
                case ClothingSize.OneSize:
                    {
                        searchData.Attributes.Add("rozmiar-rozmiar_uniwersalny,504");
                        break;
                    }
                case ClothingSize.Size24:
                    {
                        searchData.Attributes.Add("rozmiar-multi,903,946");
                        break;
                    }
                case ClothingSize.Size25:
                    {
                        searchData.Attributes.Add("rozmiar-multi,1022,596,611,612,639,945,973");
                        break;
                    }
                case ClothingSize.Size26:
                    {
                        searchData.Attributes.Add("rozmiar-multi,495,499,524,543,663,841,857,995");
                        break;
                    }
                case ClothingSize.Size27:
                    {
                        searchData.Attributes.Add("rozmiar-multi,496,500,525,544,664,842,859,974,993");
                        break;
                    }
                case ClothingSize.Size28:
                    {
                        searchData.Attributes.Add("rozmiar-multi,497,501,526,545,665,843,860,975,996");
                        break;
                    }
                case ClothingSize.Size29:
                    {
                        searchData.Attributes.Add("rozmiar-multi,498,502,527,546,666,861,971,976,997");
                        break;
                    }
                case ClothingSize.Size30:
                    {
                        searchData.Attributes.Add("rozmiar-multi,1,487,547,667,844,862,998");
                        break;
                    }
                case ClothingSize.Size31:
                    {
                        searchData.Attributes.Add("rozmiar-multi,488,492,514,548,668,863,931");
                        break;
                    }
                case ClothingSize.Size32:
                    {
                        searchData.Attributes.Add("rozmiar-multi,489,432,327,478,515,549,669,864,932");
                        break;
                    }
                case ClothingSize.Size33:
                    {
                        searchData.Attributes.Add("rozmiar-multi,336,489,516,930");
                        break;
                    }
                case ClothingSize.Size34:
                    {
                        searchData.Attributes.Add("rozmiar-multi,482,484,505,652");
                        break;
                    }
                case ClothingSize.Size35:
                    {
                        searchData.Attributes.Add("rozmiar-multi,482,484,505,652,325,490,493,653");
                        searchData.IsExactSearch = false;
                        break;
                    }
                case ClothingSize.Size36:
                    {
                        searchData.Attributes.Add("rozmiar-multi,325,490,493,653");
                        break;
                    }
                case ClothingSize.Size37:
                    {
                        searchData.Attributes.Add("rozmiar-multi,331,522,533,325,490,493,653");
                        searchData.IsExactSearch = false;
                        break;
                    }
                case ClothingSize.Size38:
                    {
                        searchData.Attributes.Add("rozmiar-multi,331,522,533");
                        break;
                    }
                case ClothingSize.Size39:
                    {
                        searchData.Attributes.Add("rozmiar-multi,331,522,533,510,617");
                        searchData.IsExactSearch = false;
                        break;
                    }
                case ClothingSize.Size40:
                    {
                        searchData.Attributes.Add("rozmiar-multi,335,617");
                        break;
                    }
                case ClothingSize.Size41:
                    {
                        searchData.Attributes.Add("rozmiar-multi,510,617");
                        searchData.IsExactSearch = false;
                        break;
                    }
                case ClothingSize.Size42:
                    {
                        searchData.Attributes.Add("rozmiar-42,510");
                        break;
                    }
                case ClothingSize.Size43:
                    {
                        searchData.Attributes.Add("rozmiar-multi,510,512");
                        searchData.IsExactSearch = false;
                        break;
                    }
                case ClothingSize.Size44:
                    {
                        searchData.Attributes.Add("rozmiar-44,512");
                        break;
                    }
                case ClothingSize.Size45:
                    {
                        searchData.Attributes.Add("rozmiar-4546,680");
                        break;
                    }
                case ClothingSize.Size46:
                    {
                        searchData.Attributes.Add("rozmiar-multi,521,680");
                        break;
                    }
                case ClothingSize.Size47:
                    {
                        searchData.Attributes.Add("rozmiar-multi,521,680,597");
                        searchData.IsExactSearch = false;
                        break;
                    }
                case ClothingSize.Size48:
                    {
                        searchData.Attributes.Add("rozmiar-48,597");
                        break;
                    }
                default:
                    searchData.IsSearchUnsupported=true;
                    break;
            }
        }

        private void MapPrice(AnswearComSearchData searchData, decimal min, decimal max)
        {
            var builder = new StringBuilder("cena,");
            if (min > 0 || max > 0)
            {
                builder.Append(min);
                builder.Append(",");

                if (max > 0)
                    builder.Append(max);
                else
                    builder.Append(Constants.MaxPrice);

                searchData.Attributes.Add(builder.ToString());
            }
        }

        private void MapColor(AnswearComSearchData searchData, ClothingColor color)
        {
            if(color == ClothingColor.Any) return;
            switch (color)
            {
                case ClothingColor.Any:
                    break;
                case ClothingColor.Black:
                    {
                        searchData.Attributes.Add("kolor-czarny,541");
                        break;
                    }
                case ClothingColor.White:
                    {
                        searchData.Attributes.Add("kolor-bialy,408");
                        break;
                    }
                case ClothingColor.Red:
                    {
                        searchData.Attributes.Add("kolor-czerwony,410");
                        break;
                    }
                case ClothingColor.Blue:
                    {
                        searchData.Attributes.Add("kolor-niebieski,415");
                        break;
                    }
                case ClothingColor.Green:
                    {
                        searchData.Attributes.Add("kolor-zielony,420");
                        break;
                    }
                case ClothingColor.Yellow:
                    {
                        searchData.Attributes.Add("kolor-zolty,421");
                        break;
                    }
                case ClothingColor.Brown:
                    {
                        searchData.Attributes.Add("kolor-brazowy,409");
                        break;
                    }
                case ClothingColor.Gray:
                    {
                        searchData.Attributes.Add("kolor-szary,418");
                        break;
                    }
                case ClothingColor.Beige:
                    {
                        searchData.Attributes.Add("kolor-bezowy,407");
                        break;
                    }
                case ClothingColor.Pink:
                    {
                        searchData.Attributes.Add("kolor-rozowy,417");
                        break;
                    }
                case ClothingColor.Navy:
                    {
                        searchData.Attributes.Add("kolor-granatowy,412");
                        break;
                    }
                case ClothingColor.Purple:
                    {
                        searchData.Attributes.Add("kolor-fioletowy,411");
                        break;
                    }
                case ClothingColor.Orange:
                    {
                        searchData.Attributes.Add("kolor-pomaranczowy,416");
                        break;
                    }
                case ClothingColor.Rainbow:
                    {
                        searchData.Attributes.Add("kolor-multikolor,414");
                        break;
                    }
            }
        }

        private void MapCategory(AnswearComSearchData searchData, ClothingSex sex, ClothingCategory category)
        {
            StringBuilder cat = new StringBuilder();
            switch (category)
            {
                case ClothingCategory.Underwear:
                    {
                        cat.Append("bielizna-");
                        cat.Append(sex == ClothingSex.Men ? "232" : "218");
                        break;
                    }
                case ClothingCategory.Blouse:
                    {
                        if (sex == ClothingSex.Women)
                            cat.Append("bluzki-209");
                        else
                        {
                            cat.Append("t-shirty-i-polo-238");
                            searchData.Attributes.Add("styl-z_dlugim_rekawem,2464");
                            searchData.IsExactSearch = false;
                        }
                        break;
                    }

                case ClothingCategory.Sweatshirt:
                    {
                        cat.Append("bluzy-");
                        cat.Append(sex == ClothingSex.Men ? "234" : "210");
                        break;
                    }
                case ClothingCategory.Jeans:
                    {
                        cat.Append("jeansy-");
                        cat.Append(sex == ClothingSex.Men ? "233" : "213");
                        break;
                    }
                case ClothingCategory.Vest:
                    {
                        if (sex == ClothingSex.Women)
                        {
                            cat.Append("zakiety-i-kamizelki-2393");
                            searchData.Attributes.Add("styl-kamizelki,2699");
                        }
                        else
                        {
                            cat.Append("marynarki-i-kamizelki-217");
                            searchData.Attributes.Add("styl-kamizelki,303");

                        }
                        break;
                    }
                case ClothingCategory.Shirt:
                    {
                        cat.Append("koszule-");
                        cat.Append(sex == ClothingSex.Men ? "240" : "2445");
                        break;
                    }
                case ClothingCategory.Polo:
                    {
                        if (sex == ClothingSex.Men)
                        {
                            cat.Append("t-shirty-i-polo-238");
                            searchData.Attributes.Add("styl-polo,2704");
                        }
                        else
                        {
                            searchData.IsSearchUnsupported = true;
                        }

                        break;
                    }
                case ClothingCategory.Jacket:
                    {
                        cat.Append("kurtki-i-plaszcze-");
                        if (sex == ClothingSex.Men)
                        {
                            cat.Append("239");
                            searchData.Attributes.Add("styl-multi,2465,258");
                        }
                        else
                        {
                            cat.Append("208");
                            searchData.Attributes.Add("styl-multi,276,314");
                        }
                        break;
                    }
                case ClothingCategory.Swimsuit:
                    {
                        if (sex == ClothingSex.Women)
                        {
                            cat.Append("stroje-kapielowe-2525");
                        }
                        else
                        {
                            cat.Append("bielizna-232");
                            searchData.Attributes.Add("styl-kapielowki,6114");
                        }

                        break;
                    }
                case ClothingCategory.Blazer:
                    {
                        if (sex == ClothingSex.Women)
                        {
                            cat.Append("zakiety-i-kamizelki-2393");
                            searchData.Attributes.Add("styl-zakiety,2698");
                        }
                        else
                        {
                            cat.Append("marynarki-i-kamizelki-217");
                            searchData.Attributes.Add("styl-marynarki,2336");

                        }
                        break;
                    }
                case ClothingCategory.Nightwear:
                    {
                        if (sex == ClothingSex.Women)
                        {
                            cat.Append("bielizna-218");
                            searchData.Attributes.Add("styl-pizamy,2535");
                        }
                        else
                        {
                            cat.Append("bielizna-232");
                            searchData.Attributes.Add("styl-pizamy,5793");

                        }
                        break;
                    }
                case ClothingCategory.Coat:
                    {
                        cat.Append("kurtki-i-plaszcze-");
                        if (sex == ClothingSex.Men)
                        {
                            cat.Append("239");
                            searchData.Attributes.Add("styl-plaszcze,2703");
                        }
                        else
                        {
                            cat.Append("208");
                            searchData.Attributes.Add("styl-plaszcze,278");
                        }
                        break;
                    }
                case ClothingCategory.Trousers:
                    {
                        cat.Append(sex == ClothingSex.Women ? "spodnie-i-legginsy-211" : "spodnie-237");
                        break;
                    }
                case ClothingCategory.Shorts:
                    {
                        cat.Append(sex == ClothingSex.Women ? "szorty-2382" : "szorty-2422");
                        break;
                    }
                case ClothingCategory.Skirt:
                    {
                        if (sex == ClothingSex.Women)
                        {
                            cat.Append("spodnice-216");
                        }
                        else
                        {
                            searchData.IsSearchUnsupported = true;
                        }
                        break;
                    }
                case ClothingCategory.Dress:
                    {
                        if (sex == ClothingSex.Women)
                        {
                            cat.Append("sukienki-i-tuniki-212");
                        }
                        else
                        {
                            searchData.IsSearchUnsupported = true;
                        }
                        break;
                    }
                case ClothingCategory.Sweater:
                    {
                        cat.Append(sex == ClothingSex.Women ? "swetry-206" : "swetry-236");
                        break;
                    }
                case ClothingCategory.Top:
                    {
                        if (sex == ClothingSex.Women)
                        {
                            cat.Append("topy-214");
                        }
                        else
                        {
                            cat.Append("t-shirty-i-polo-238");
                            searchData.Attributes.Add("styl-bez_rekawow,337");
                            searchData.IsExactSearch = false;
                        }
                        break;
                    }
                case ClothingCategory.Tshirt:
                    {
                        if (sex == ClothingSex.Men)
                        {
                            cat.Append("t-shirty-i-polo-238");
                            searchData.Attributes.Add("styl-tshirty,2720");
                        }
                        else
                        {
                            cat.Append("topy-214");
                            searchData.Attributes.Add("styl-z_krotkim_rekawem,274");
                        }
                        break;
                    }
                case ClothingCategory.Suit:
                    {
                        cat.Append(sex == ClothingSex.Women ? "zakiety-i-kamizelki-2393" : "marynarki-i-kamizelki-217");
                        searchData.IsExactSearch = false;
                        break;
                    }
                default:
                    searchData.IsSearchUnsupported = true;
                    break;
            }

            searchData.Category = cat.ToString();
        }
    }
}